//
//  My_editAreaVC.m
//  daojiVideo
//
//  Created by tikeios_002 on 2024/5/21.
//

#import "My_editAreaVC.h"

@interface My_editAreaVC ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *footView;
@property (weak, nonatomic) IBOutlet UILabel *nav_title;
@property (weak, nonatomic) IBOutlet UIButton *nav_close;
@property (weak, nonatomic) IBOutlet UIButton *nav_save;
@property (weak, nonatomic) IBOutlet UIPickerView *areaPick;
@property (weak, nonatomic) IBOutlet UISwitch *openSwitch;
@property (weak, nonatomic) IBOutlet UILabel *tipLabel;

@property (strong, nonatomic)NSArray * countryArray;//省份
@property (strong, nonatomic)NSArray * stateArray;//省份
@property (strong, nonatomic)NSArray * cityArray;//城市
@property (strong, nonatomic)NSDictionary * countryDic;//国家
@property (strong, nonatomic)NSDictionary * stateDic;//省份
@property (strong, nonatomic)NSDictionary * cityDic;//城市
@property (assign, nonatomic)NSInteger nowCurrent;
@end

@implementation My_editAreaVC

-(void)showTextViewNavViewTo1{
    NSMutableArray * showTextViewNavViewNameArrTo1 = [NSMutableArray new];
    UIView * showTextViewNavViewViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewNavViewViewTo1.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:showTextViewNavViewViewTo1];
    UILabel * showTextViewNavViewLabelTo1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    NSInteger showTextViewNavViewNumberTo1 = arc4random_uniform(20);
    NSLog(@"：%ld", (long)showTextViewNavViewNumberTo1);
    showTextViewNavViewLabelTo1.text = [NSString stringWithFormat:@"%ld",(long)showTextViewNavViewNumberTo1];
    [showTextViewNavViewLabelTo1 addSubview:showTextViewNavViewLabelTo1];
    
    UIImageView * showTextViewNavViewLabelTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [showTextViewNavViewViewTo1 addSubview:showTextViewNavViewLabelTo2];
    
}
- (void)showOldAddNavViewToMainOldView1{
    UIView * oldBgSS = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    oldBgSS.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:oldBgSS];
    
    UIButton * sendBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreen_W-75, kStatusBarHeight+10, 60, 46)];
    sendBtn.layer.cornerRadius = 3;
    sendBtn.backgroundColor = [UIColor whiteColor];
    [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
    [sendBtn setTitleColor:ColorHex(@"#333333") forState:UIControlStateNormal];
    sendBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [sendBtn addTarget:self action:@selector(sendTap) forControlEvents:UIControlEventTouchUpInside];
    [oldBgSS addSubview:sendBtn];
    
    UIView * sendBg = [[UIView alloc]initWithFrame:CGRectMake(15, kStatusBarHeight+66, KScreen_W-30, (KScreen_H-KSafeAreaBottomHeight-25-(kStatusBarHeight+66))/2)];
    sendBg.backgroundColor = [UIColor whiteColor];
    sendBg.layer.cornerRadius = 3;
    [oldBgSS addSubview:sendBg];
    
    UILabel * sendTip = [[UILabel alloc]initWithFrame:CGRectMake(0, (sendBg.frame.size.height-40)/2, sendBg.frame.size.width, 40)];
    sendTip.text = @"发送区";
    sendTip.textColor = ColorHex(@"#f0f0f0");
    sendTip.textAlignment = NSTextAlignmentCenter;
    sendTip.font = [UIFont systemFontOfSize:28];
    [sendBg addSubview:sendTip];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    self.footView.backgroundColor = viewback_black;
    self.footView.layer.cornerRadius = 18;
    self.footView.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
    
    self.openSwitch.transform = CGAffineTransformMakeScale(.81, .81);
    
    [self.nav_close setTitle:NSLocalizedString(@"取消", nil) forState:UIControlStateNormal];
    [self.nav_save setTitle:NSLocalizedString(@"保存", nil) forState:UIControlStateNormal];
    self.nav_title.text = NSLocalizedString(@"地区", nil);
    self.tipLabel.text = NSLocalizedString(@"展示地区标签", nil);
    
    self.areaPick.delegate = self;
    self.areaPick.dataSource = self;
    
    if ([[QZUserInfoModel share].is_show_area intValue] == 0) {
        self.openSwitch.on = NO;
    }
    if([QZUserInfoModel share].areaArray == nil){
        [SVProgressHUDTool showWithStatus:nil];
        [self requestAreaDatas];
    }else{
        [self showValueArea];
    }
}
- (IBAction)closeClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)saveClick:(id)sender {
    KMutableDic(sendDic)
    if(self.countryDic != nil){
        [sendDic setObject:self.countryDic[@"name"] forKey:@"country"];
    }
    if(self.stateDic != nil){
        [sendDic setObject:self.stateDic[@"name"] forKey:@"province"];
    }else{
        [sendDic setObject:@"" forKey:@"province"];
    }
    if(self.cityDic != nil){
        [sendDic setObject:self.cityDic[@"name"] forKey:@"city"];
    }else{
        [sendDic setObject:@"" forKey:@"city"];
    }
    if (self.openSwitch.on) {
        [sendDic setObject:@(0) forKey:@"is_show_home"];
    }else{
        [sendDic setObject:@(1) forKey:@"is_show_home"];
    }
    KWeakSelf
    [YBNetBase userEditCitySetAreaRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            [[QZUserInfoModel share] setValuesForKeysWithDictionary:data];
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
        }else{
            [YBMsgPop showPop:msg];
        }
    }];
}
- (void)requestAreaDatas{
    KMutableDic(sendDic)
    KWeakSelf
    [YBNetBase userCityAreaRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            [SVProgressHUDTool dismiss];
            [QZUserInfoModel share].areaArray = data[@"list"];
            [weakSelf showValueArea];
            [SVProgressHUDTool dismiss];
        }else{
            [YBMsgPop showPop:msg];
        }
    }];
}
- (void)showValueArea{
    self.countryArray = [QZUserInfoModel share].areaArray;
    if([QZUserInfoModel share].area_str.length == 0){
        int c_index = 0;
        for (NSDictionary * dic in self.countryArray) {
            if([dic[@"name"] containsString:@"中国"]){
                self.countryDic = dic;
                
                break;
            }else{
                c_index++;
            }
        }
        self.stateArray = self.countryArray[c_index][@"_child"];
        self.cityArray = self.stateArray[0][@"_child"];
       
        self.stateDic = self.stateArray[0];
        self.cityDic = self.cityArray[0];
        [self.areaPick reloadAllComponents];
        [self.areaPick selectRow:c_index inComponent:0 animated:NO];
    }else{
        int c_index = 0;
        int p_index = 0;
        int s_index = 0;
        for (NSDictionary * dic in self.countryArray) {
            if([dic[@"name"] containsString:[QZUserInfoModel share].country_str]){
                self.countryDic = dic;
                self.stateArray = dic[@"_child"];
                
                break;
            }else{
                c_index++;
            }
        }
        if([QZUserInfoModel share].province_str.length != 0){
            for (NSDictionary * dic in self.stateArray) {
                if([dic[@"name"] containsString:[QZUserInfoModel share].province_str]){
                    self.stateDic = dic;
                    self.cityArray = dic[@"_child"];
                    
                    break;
                }else{
                    p_index++;
                }
            }
        }
        if([QZUserInfoModel share].city_str.length != 0){
            for (NSDictionary * dic in self.cityDic) {
                if([dic[@"name"] containsString:[QZUserInfoModel share].city_str]){
                    self.cityDic = dic;
                    
                    break;
                }else{
                    s_index++;
                }
            }
        }
        
        [self.areaPick reloadAllComponents];
        [self.areaPick selectRow:c_index inComponent:0 animated:NO];
        [self.areaPick selectRow:p_index inComponent:1 animated:NO];
        [self.areaPick selectRow:s_index inComponent:2 animated:NO];
    }
}
#pragma mark - picker
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return self.countryArray.count;
    }else if (component == 1) {
        return self.stateArray.count;
    }else if (component == 2) {
        return self.cityArray.count;
    }
    return 0;
}

- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        return self.countryArray[row][@"name"];
    }else if (component == 1) {
        return self.stateArray[row][@"name"];
    }else if (component == 2) {
        return self.cityArray[row][@"name"];
    }
    return @"";
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        self.stateArray= self.countryArray[row][@"_child"];
        self.cityArray = self.stateArray[0][@"_child"];
        [pickerView reloadComponent:1];
        [pickerView reloadComponent:2];
        [pickerView selectRow:0 inComponent:1 animated:YES];
        [pickerView selectRow:0 inComponent:2 animated:YES];
        self.countryDic = self.countryArray[row];
        if (self.stateArray.count != 0) {
            self.stateDic = self.stateArray[0];
        }else{
            self.stateDic = nil;
        }
        if (self.cityArray.count != 0) {
            self.cityDic = self.cityArray[0];
        }else{
            self.cityDic = nil;
        }
    }else if (component == 1) {
        self.cityArray = self.stateArray[row][@"_child"];
        [pickerView reloadComponent:2];
        [pickerView selectRow:0 inComponent:2 animated:YES];
        self.stateDic = self.stateArray[row];
        if (self.cityArray.count != 0) {
            self.cityDic = self.cityArray[0];
        }else{
            self.cityDic = nil;
        }
    }else if (component == 2) {
        if (self.cityArray.count != 0) {
            self.cityDic = self.cityArray[row];
        }else{
            self.cityDic = nil;
        }
        
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 34.0;
}


- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, KScreen_W/3, 34)];
    myView.backgroundColor = [UIColor clearColor];
    myView.textAlignment = NSTextAlignmentCenter;
    myView.font = [UIFont systemFontOfSize:16];
    myView.textColor = [UIColor whiteColor];
    if (component == 0) {
        myView.text = self.countryArray[row][@"name"];
    }else if (component == 1) {
        myView.text = self.stateArray[row][@"name"];
    }else if (component == 2) {
        myView.text = self.cityArray[row][@"name"];
    }
    return myView;
}
#pragma mark - HWPanModalPresentable
- (BOOL)shouldRespondToPanModalGestureRecognizer:(UIPanGestureRecognizer *)panGestureRecognizer{
    CGPoint loc = [panGestureRecognizer locationInView:self.view];
    return !CGRectContainsPoint(self.areaPick.frame, loc);

}
- (PanModalHeight)longFormHeight {
    return PanModalHeightMake(PanModalHeightTypeMax, 0);
}

- (HWBackgroundConfig *)backgroundConfig{
    HWBackgroundConfig *config = [HWBackgroundConfig new];
    config.backgroundAlpha = 0.7;
    return config;
}

- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

- (CGFloat)cornerRadius{
    return 16.0;
}

- (CGFloat)topOffset{
    return KScreen_H-(355+KSafeAreaBottomHeight);
}

- (PanModalHeight)shortFormHeight {
    return [self longFormHeight];
}
- (BOOL)isAutoHandleKeyboardEnabled{
    return NO;
}

- (BOOL)showDragIndicator {
    return NO;
}
- (BOOL)allowsDragToDismiss {
    return NO;
}
-(BOOL)isPanScrollEnabled{
    return NO;
}
@end
